package com.uetoken.cn.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.uetoken.cn.R;
import com.uetoken.cn.utils.PhotoUtils;

/**
 * @author 谢杨
 * @time 2018/7/26 10:25
 * @class 描述:各类弹框
 */
public class DialogHelper {
    public static void showRationaleDialog(final PermissionUtils.OnRationaleListener.ShouldRequest shouldRequest) {
        Activity topActivity = ActivityUtils.getTopActivity();
        if (topActivity == null) {
            return;
        }
        new QMUIDialog.MessageDialogBuilder(topActivity)
                .setTitle(R.string.str_permission_related)
                .setMessage(R.string.str_permission_rationale_message)
                .addAction(R.string.str_cancel, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        shouldRequest.again(false);
                    }
                })
                .addAction(R.string.str_confirm, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        shouldRequest.again(true);
                    }
                })
                .show();

    }

    public static void showOpenAppSettingDialog() {
        Activity topActivity = ActivityUtils.getTopActivity();
        if (topActivity == null) {
            return;
        }
        new QMUIDialog.MessageDialogBuilder(topActivity)
                .setTitle(R.string.str_permission_related)
                .setMessage(R.string.str_permission_denied_forever_message)
                .addAction(R.string.str_cancel, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(R.string.str_confirm, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        PermissionUtils.launchAppDetailsSettings();
                    }
                })
                .show();
    }

    /**
     * 显示拍照或选择图库的弹窗
     *
     * @param photoUtils
     */
    public static void showCameraDialog(final PhotoUtils photoUtils) {
        final Activity topActivity = ActivityUtils.getTopActivity();
        if (topActivity == null) {
            return;
        }

        final Dialog dialog = new Dialog(topActivity, R.style.photo_select_dialog);
        View mView = LayoutInflater.from(topActivity).inflate(R.layout.dialog_photo_select, null);
        TextView mTvTakePhoto = mView.findViewById(R.id.tv_dialog_photo_select_take_photo);
        TextView mTvViewAlbum = mView.findViewById(R.id.tv_dialog_photo_select_view_album);
        dialog.setContentView(mView);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.y = 20;
        dialogWindow.setAttributes(lp);
        dialog.show();

        mTvTakePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                photoUtils.takePhoto(topActivity);
            }
        });

        mTvViewAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                photoUtils.albumSelect(topActivity);
            }
        });
    }

    public static void showMessageDialog(Context context, CharSequence title, CharSequence message, CharSequence negativeStr, CharSequence positiveStr,
                                         QMUIDialogAction.ActionListener negativeListener, QMUIDialogAction.ActionListener positiveListener) {
        new QMUIDialog.MessageDialogBuilder(context)
                .setTitle(ObjectUtils.isEmpty(title) ? "" : title.toString())
                .setMessage(message)
                .addAction(negativeStr, negativeListener)
                .addAction(positiveStr, positiveListener).show();
    }

    public static Dialog showOpenFingerprintPaymentDialog(Context mContext, View.OnClickListener openListener, View.OnClickListener cancelListener) {
        View view = View.inflate(mContext, R.layout.dialog_fingerprint_payment_open, null);
        final Dialog dialog = new Dialog(mContext, R.style.loading_dialog);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        ImageView fingerprintIv = view.findViewById(R.id.fingerprintIv);
        LinearLayout btnLayout = view.findViewById(R.id.btnLayout);
        Button openFingerprintBtn = view.findViewById(R.id.openFingerprintBtn);
        TextView thinkAgainTv = view.findViewById(R.id.thinkAgainTv);
        int width = SizeUtils.getMeasuredWidth(fingerprintIv);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btnLayout.getLayoutParams();
        params.width = width;
        btnLayout.setLayoutParams(params);
        openFingerprintBtn.setOnClickListener(openListener);
        thinkAgainTv.setOnClickListener(cancelListener);
        return dialog;
    }

    public static Dialog showFingerprintVerificationDialog(Context mContext, View.OnClickListener cancelListener) {
        View view = View.inflate(mContext, R.layout.dialog_fingerprint_verification_fragment, null);
        final Dialog dialog = new Dialog(mContext, R.style.loading_dialog);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        TextView cancelTv = view.findViewById(R.id.cancelTv);
        cancelTv.setOnClickListener(cancelListener);
        return dialog;
    }

    public static Dialog showTipDialog(Context mContext, String message, String btnstr, View.OnClickListener yesListener) {
        View view = View.inflate(mContext, R.layout.dialog_tip_view, null);
        final Dialog dialog = new Dialog(mContext, R.style.loading_dialog);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        TextView messageTv = view.findViewById(R.id.messageTv);
        if (!ObjectUtils.isEmpty(message)) {
            messageTv.setText(message);
        }
        Button knownBtn = view.findViewById(R.id.knownBtn);
        if (!ObjectUtils.isEmpty(btnstr)) {
            knownBtn.setText(btnstr);
        }
        if (yesListener != null) {
            knownBtn.setOnClickListener(yesListener);
        }
        return dialog;
    }

    public static Dialog showPrivacyDialog(Context mContext, String title, SpannableString message, String negativeStr, String positiveStr, View.OnClickListener negativeListener, View.OnClickListener positiveListener) {
        View view = View.inflate(mContext, R.layout.dialog_privacyprotocol_fragment, null);
        final Dialog dialog = new Dialog(mContext, R.style.loading_dialog);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        TextView titleTv = view.findViewById(R.id.titleTv);
        TextView msgTv = view.findViewById(R.id.msgTv);
        TextView disagreeTv = view.findViewById(R.id.disagreeTv);
        TextView agreeTv = view.findViewById(R.id.agreeTv);

        if (!ObjectUtils.isEmpty(title)) {
            titleTv.setText(title);
        }
        msgTv.setMovementMethod(LinkMovementMethod.getInstance());
        if (!ObjectUtils.isEmpty(message)) {
            msgTv.setText(message);
        }
        if (!ObjectUtils.isEmpty(negativeStr)) {
            disagreeTv.setText(negativeStr);
        }
        if (!ObjectUtils.isEmpty(positiveStr)) {
            agreeTv.setText(positiveStr);
        }
        if (!ObjectUtils.isEmpty(negativeListener)) {
            disagreeTv.setOnClickListener(negativeListener);
        }
        if (!ObjectUtils.isEmpty(positiveListener)) {
            agreeTv.setOnClickListener(positiveListener);
        }
        return dialog;
    }
}
